package com.youngjun.user.service.impl;

import com.youngjun.common.mybatis.page.PageParams;
import com.youngjun.common.mybatis.plugin.PageHelper;
import com.youngjun.user.mapper.InterfaceMapper;
import com.youngjun.user.pojo.domain.Interface;
import com.youngjun.user.pojo.dto.InterfaceDTO;
import com.youngjun.user.pojo.dto.query.FunctionInterfaceQueryDTO;
import com.youngjun.user.pojo.dto.query.InterfaceQueryDTO;
import com.youngjun.user.pojo.vo.InterfaceVO;
import com.youngjun.user.service.FunctionInterfaceService;
import com.youngjun.user.service.InterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class InterfaceServiceImpl implements InterfaceService {

    @Autowired
    InterfaceMapper interfaceMapper;

    @Autowired
    FunctionInterfaceService functionInterfaceService;

    @Override
    public InterfaceMapper getMapper() {
        return interfaceMapper;
    }

    @Override
    public List<InterfaceVO> queryUnAssignedList(FunctionInterfaceQueryDTO dto, PageParams pageParams) {
        PageHelper.startPage(pageParams);
        return getMapper().findUnAssignedList(dto)
                .stream()
                .map(item->new InterfaceVO().mapFrom(item))
                .collect(Collectors.toList());
    }

    @Override
    public List<InterfaceVO> queryAssignedList(FunctionInterfaceQueryDTO dto) {
        return getMapper().findAll(dto)
                .stream()
                .map(item->new InterfaceVO().mapFrom(item))
                .collect(Collectors.toList());
    }

    @Override
    public List<InterfaceVO> queryList(InterfaceQueryDTO dto, PageParams pageParams) {
        FunctionInterfaceQueryDTO params = new FunctionInterfaceQueryDTO().mapFrom(dto);
        PageHelper.startPage(pageParams);
        return getMapper().findAll(params)
                .stream()
                .map(item->new InterfaceVO().mapFrom(item))
                .collect(Collectors.toList());
    }

    @Override
    public InterfaceVO create(InterfaceDTO dto) {
        Interface params = new Interface().mapFrom(dto);
        insert(params);
        return new InterfaceVO().mapFrom(params);
    }

    @Override
    public InterfaceVO modify(InterfaceDTO dto) {
        Interface params = new Interface().mapFrom(dto);
        updateByPrimaryKey(params);
        return new InterfaceVO().mapFrom(params);
    }

    @Override
    public void delete(Long interfaceId) {
        deleteWithLogic(interfaceId);
        functionInterfaceService.deleteWithLogicByInterfaceId(interfaceId);
    }

}